package curator.leaderSelector;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.leader.LeaderLatch;

public class LeaderLatchTest3 {

    private static String LOCK_PATH = "/leader_latch";

    public static void main(String[] args) throws Exception {
        CuratorFramework client = CuratorClientUtil.getZkClient();
        CuratorFramework client2 = CuratorClientUtil.getZkClient();

        LeaderLatch leaderLatch = new LeaderLatch(client, LOCK_PATH, "client-1");
        LeaderLatch leaderLatch2 = new LeaderLatch(client2, LOCK_PATH, "client-2");

        leaderLatch.start();
        leaderLatch2.start();

        Thread.sleep(5000);
        if (leaderLatch.hasLeadership()) {
            System.out.println(leaderLatch.getId() + "获取到leader。。");
        }

        Thread.sleep(5000);
        if (leaderLatch2.hasLeadership()) {
            System.out.println(leaderLatch2.getId() + "获取到leader...");
        }
    }
}
